package core;

import ibm.util.BrowserOps;
import resources.core.LoginTasksHelper;
import authorization.login.ILoginForm;

import com.ibm.gsk.ikeyman.error.NotImplementedException;

import core.ControlsMapping.ChooseNamespace;
import core.ControlsMapping.LogInFormWebSphere;
import core.ControlsMapping.LoginFormCognos;
import core.enums.NameSpaces;
import core.enums.Reports;
import core.exceptions.ResultFailureException;

/**
 * Description : Functional Test Script
 * 
 * @author angel.tsvetkov
 */
public class LoginTasks extends LoginTasksHelper
{
    /**
     * Script Name : <b>LoginTasks</b> Generated : <b>22.06.2010 13:07:12</b> Description : Functional Test Script
     * Original Host : WinNT Version 5.1 Build 2600 (S)
     * 
     * @since 2010/06/22
     * @author angel.tsvetkov
     */

    private static int sleepTime = 5;

    /**
     * 
     * @return true if browser is started successful. First it close all browsers and browsers dialogs
     */
    public static boolean openBrowser(String reportURL)
    {
        boolean result = true;

        // Attempt to close all the open browsers, just in case
        try
        {
            BrowserOps.closeAllBrowserDialogs();
            TimeWait(3);
        }
        catch (Exception e)
        {
            // do nothing, this was precautionary, but log i just so we know
        	setErrorComment("Unexpected assertion on trying to close all browsers!");
        }
        try
        {
            BrowserOps.closeAllBrowsers();
            TimeWait(3);
        }
        catch (Exception e)
        {
            // do nothing, this was precautionary, but log it just so we know
            setErrorComment("Unexpected assertion on trying to close all browsers!");
        }
        try
        {
            startBrowser(reportURL);
        }
        catch (Exception e)
        {
        	setErrorComment("Unable to start the browser");
            result = false;
        }
        // Sleep a little to make sure the browser is done loading
        sleep(sleepTime);

        return result;
    }

    /**
     * 
     * @return click TAB+TAB+Enter. Works form IE and Mozilla
     */
    public static void acceptCertificate()
    {
        sleep(sleepTime);
        getScreen().inputKeys("{tab}{tab}{enter}");
    }
    
    private static void chooseNamespace(core.enums.NameSpaces namespace) throws ResultFailureException
    {
		ChooseNamespace.namespace_dropdown.select(namespace.getName());
		
		ChooseNamespace.ok_button.click();	
    }
    
    public static boolean loginUser(String userName, String password, boolean isCognosReport)
    {
        boolean result = true;
        String taskReport = null;
        int ENCRYPT_MASK = 69;
        
        try
        {
        	if(isCognosReport)
        	{
	        	taskReport = " \n Enter user name: " + userName;
	        	chooseNamespace(NameSpaces.IBMIntranet);
	        	
	        	TimeWait(2);
	        	
	            taskReport = " \n Enter user name: " + userName;
	            LoginFormCognos.username_textbox.enter(userName);
	
	            taskReport += "\n Enter password!";
	            String pass = Functions.encrypt(password, ENCRYPT_MASK);
	            LoginFormCognos.password_textbox.enter(pass);
	
	            taskReport += "\n Press 'Login' button! ";
	            ChooseNamespace.ok_button.click();
        	}
        	else
        	{
	            taskReport = " \n Enter user name: " + userName;
	            LogInFormWebSphere.username_textbox.enter(userName);
	
	            taskReport += "\n Enter password!";
	            String pass = Functions.encrypt(password, ENCRYPT_MASK);
	            LogInFormWebSphere.password_textbox.enter(pass);
	
	            taskReport += "\n Press 'Login' button! ";
	            LogInFormWebSphere.submit_button.click();
        	}
        }
        catch (ResultFailureException e)
        {
            setComment(e.getError());
            result = false;
        }
        setTaskResult("Login Task", result, taskReport);

        return result;
    }
}
